package com.isunland.managesystem.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.MyFriendListAdapter;
import com.isunland.managesystem.base.Base;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.MyFriend;
import com.isunland.managesystem.entity.MyFriendOriginal;
import com.isunland.managesystem.utils.SharedPreferencesUtil;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/3/16 18:40
 */
public class AgriculturalMyFriendListFragmen extends BaseListFragment {

  private ArrayList<MyFriend> mList;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
    setBack(true);
    setTitleCustom(R.string.myFriend);
    mList = new ArrayList<>();
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_query, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        return true;
      case R.id.menu_item_query:
        //添加好友
        Intent add = new Intent(getActivity(), AddMyFrendActivity.class);
        startActivity(add);

        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Override public String getUrl() {
    return ApiConst.URL_MY_FRIEND;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<>();
    params.put("jobno", CurrentUser.newInstance(getActivity()).getJobNumber());
    params.put("subCode",
        SharedPreferencesUtil.getString(getActivity(), SharedPreferencesUtil.KEY_FORUM_SUBCODE,
            ""));
    return params;
  }

  @Override public void resolveResponse(String json) {
    MyFriendOriginal original = new Gson().fromJson(json, MyFriendOriginal.class);
    if (original.getResult() == Base.SUCCESS && original.getRows() != null) {
      //获取列表成功
      mList.clear();
      mList.addAll(original.getRows());
      MyFriendListAdapter mAdapter =
          new MyFriendListAdapter(getActivity(), mList, null, MyFriendListAdapter.FRIEND);
      setListAdapter(mAdapter);
    } else {
      ToastUtil.show("获取列表失败");
    }
  }

  //可以查看专家发布的帖子和回复
  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    MyFriend content = mList.get(position - 1);
    Intent intent = new Intent(getActivity(), AgriculturalMyExpertPagerActivity.class);
    intent.putExtra(AgriculturalForumListFragment.EXTRA_FROM, AgriculturalForumListFragment.PERSON);
    intent.putExtra(AgriculturalForumListFragment.EXTRA_JOBNO, content.getToAddJobno());
    startActivity(intent);
    super.onListItemClick(l, v, position, id);
  }
}

